package marek.barak.classes;

import java.io.Serializable;
import java.util.List;
import java.util.ListIterator;
import java.util.Map;
import java.util.ArrayList;

public class ActionMade implements Serializable {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = -5055584717324949324L;
	private int amounth;
	private int time;
	private String name;
	
	@Override
	public String toString(){
		return "Name: "+name+", Amounth: " + amounth + ", Time: "+time;
	}
	
	public static ActionMade getInstance(Action A,int amounth){
		int tmpAmounth = amounth;
		int time = 0;
		while(tmpAmounth>0){
			List<Map.Entry<Integer,Integer>> list = new ArrayList<Map.Entry<Integer,Integer>>(A.getTime().entrySet());
			for(ListIterator<Map.Entry<Integer,Integer>> itr =list.listIterator();itr.hasNext();){
				if(tmpAmounth > itr.next().getKey()){
					if (!itr.hasNext()){
					tmpAmounth-=itr.previous().getKey();
					time +=itr.next().getValue();
					}else continue;
				}else{
					time +=itr.previous().getValue();
					tmpAmounth=0;
					break;
				}
			}	
		}
		
		return new ActionMade(amounth,time,A.getName());
	}
	public int getAmounth() {
		return amounth;
	}
	public int getTime() {
		return time;
	}
	public String getName() {
		return name;
	}
	private ActionMade(int amounth, int time, String name) {
		super();
		this.amounth = amounth;
		this.time = time;
		this.name = name;
	}

	

	


}
